package com.wyix.create;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 美食评分计算器
 *
 * @author wangdx
 * @since 2025/8/4
 */
public class FoodRatingCalculator implements Callable<Double> {
    private String foodName;

    public FoodRatingCalculator(String foodName) {
        this.foodName = foodName;
    }

    @Override
    public Double call() throws Exception {
        System.out.println("开始计算 " + foodName + " 的评分...");
        // 模拟评分计算过程
        Thread.sleep(2000);

        Random random = new Random();
        double taste = random.nextDouble() * 5;  // 口味评分(0-5)
        double presentation = random.nextDouble() * 3;  // 外观评分(0-3)
        double service = random.nextDouble() * 2;  // 服务评分(0-2)
        double finalScore = taste + presentation + service;

        System.out.println(foodName + " 评分计算完成!");
        return Math.round(finalScore * 10) / 10.0;  // 保留一位小数
    }

    public static void main(String[] args) {
        List<FutureTask<Double>> tasks = new ArrayList<>();
        String[] foods = {"火锅", "烧烤", "寿司", "披萨"};

        // 创建并启动所有评分任务
        for (String food : foods) {
            FutureTask<Double> task = new FutureTask<>(new FoodRatingCalculator(food));
            tasks.add(task);
            new Thread(task).start();
        }

        // 获取并打印结果
        System.out.println("\n===== 美食最终评分 =====");
        for (int i = 0; i < tasks.size(); i++) {
            try {
                System.out.println(foods[i] + ": " + tasks.get(i).get() + " 分");
            } catch (InterruptedException | ExecutionException e) {
                System.out.println(foods[i] + ": 评分计算失败");
            }
        }
    }
}
